package math.chiu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import math.chiu.entity.*;

public class ListStudent extends HttpServlet
{
	private final String SQL_QUERY_STUDENT = "SELECT * FROM STUDENT";
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		doProcess(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		doProcess(request, response);
	}
	public void doProcess(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		response.setContentType("application/json");
		Connection connection = null;
		PreparedStatement studentStatement = null;
		ResultSet studentResult = null;
		List<Student> students = new ArrayList<Student>();
		Student student = null;
		
		try
		{
			connection = getConnection();
			studentStatement = connection.prepareStatement(SQL_QUERY_STUDENT);
			studentResult = studentStatement.executeQuery();
			while(studentResult.next())
			{
				student = new Student();
				student.setName(studentResult.getString("name"));
				student.setAge(Integer.parseInt(studentResult.getString("age")));
				students.add(student);
			}
		}
		catch(NumberFormatException e)
		{
			e.printStackTrace();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			closeStatement(studentStatement);
			closeConnection(connection);
		}
		
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		
//		for(Student s : students)
//		{
//			String json = gson.toJson(s);
//			out.println(json);
//		}
		out.println(gson.toJson(students));
		out.close();
	}
	
	
	/////////////////////
	
	private final String getConnectionString()
	{
		return getServletContext().getInitParameter("connectionString");
	}
	private final String getUserName()
	{
		return getServletContext().getInitParameter("userName");
	}
	private final String getPassword()
	{
		return getServletContext().getInitParameter("password");
	}
	
	private final Connection getConnection() throws SQLException
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		return DriverManager.getConnection(getConnectionString(), getUserName(), getPassword());
	}
	
	private final void closeConnection(Connection connection)
	{
		if(connection != null)
		{
			try
			{
				connection.close();
			}
			catch(SQLException e)
			{
				e.printStackTrace();
				throw new RuntimeException("close connection failed");
			}
		}
	}
	
	private final void closeStatement(PreparedStatement statement)
	{
		if(statement != null)
		{
			try
			{
				statement.close();
			}
			catch(SQLException e)
			{
				e.printStackTrace();
				throw new RuntimeException("close statement failed");
			}
		}
	}
}
